package com.itheima.info.manager.controller;

import com.itheima.info.manager.domain.Student;
import com.itheima.info.manager.factroy.SystemFactory;
import com.itheima.info.manager.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public abstract class BaseStudentController {
    StudentService studentService = SystemFactory.getStudentService();

    Scanner sc = new Scanner(System.in);

    public final void start() {
        loop:
        while (true) {
            //1. 展示欢迎页面,用输出语句完成主界面的编写
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            //3. 根据用户的选择执行对应的操作,用switch语句完成操作的选择
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    break loop;
                default:
                    System.out.println("输入有误，重新输入");
                    break;

            }
        }
    }
    // 修改学生
    public final void updateStudent(){
        ArrayList<Student> allStudent = studentService.findAllStudent();
        if (allStudent.size() == 0) {
            System.out.println("查无信息，请先添加信息");
            return;
        }

        String id = inputStudentId();
        Student student = getStudentInfo(id);
        studentService.updateStudent(id,student);
        System.out.println("修改成功");

    }

    // 删除学生
    public final void deleteStudentById(){
        ArrayList<Student> allStudent = studentService.findAllStudent();
        if (allStudent.size() == 0) {
            System.out.println("查无信息，请先添加信息");
            return;
        }
        String id = inputStudentId();
        studentService.deleteStudentById(id);
        System.out.println("删除成功");
    }

    // 查看所有学生
    public final void findAllStudent() {
        ArrayList<Student> allStudent = studentService.findAllStudent();
        if (allStudent.size() == 0) {
            System.out.println("查无信息，请先添加信息");
            return;
        }
        Student student = new Student();
        System.out.println("学号\t\t姓名\t\t年龄\t生日");
        for (int i = 0; i < allStudent.size(); i++) {
            student = allStudent.get(i);
            System.out.println(student.getId() + "\t" + "\t" + student.getName() + "\t" + "\t"
                    + student.getAge() + "\t\t" + student.getBirthday());
        }

    }

    // 添加学生
    public final void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生ID");
            id = sc.next();
            boolean flag = studentService.isExits(id);
            if (flag) {
                System.out.println("此学号已经存在，请重新输入");
            } else {
                break;
            }
        }
        Student student = getStudentInfo(id);
        studentService.addStudent(student);
        System.out.println("添加成功");
    }

    // 封装学生信息
    private final Student getStudentInfo(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();

        return new Student(id, name, age, birthday);
    }

    // 获取ID
    private final String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入ID");
            id = sc.next();
            boolean flag = studentService.isExits(id);
            if (!flag) {
                System.out.println("学号不存在，重新输入");
            } else {
                break;
            }
        }
        return id;
    }

}
